/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package processor;

/**
 *
 * @author DYNG09
 */
public class DetectQuery {

    private String query;
    private String singleQuery;

    public DetectQuery() {
        this.query = null;
    }

    public DetectQuery(String q) {
        KeyWord kw = new KeyWord(q);
        this.query = kw.changKeyWordToLowerCase();
    }

    /**
     * check if query is SELECT, INSERT, UPDATE or DELETE statement
     *
     * @return 0-SELECT, 1-INSERT, 2-UPDATE, 3-DELETE, (-1)-ERROR
     */
    public int detect() {
        singleQuery = singleQuery.trim();
        if (singleQuery == null || singleQuery.equals("")) {
            return Constant.ERROR;
        }
        if (singleQuery.startsWith("select")) {
            if (singleQuery.substring(6, singleQuery.length()).contains(" select ")) {
                return Constant.SELECT_LV2;
            }
            return Constant.SELECT;
        }
        if (singleQuery.startsWith("insert")) {
            return Constant.INSERT;
        }
        if (singleQuery.startsWith("update")) {
            return Constant.UPDATE;
        }
        if (singleQuery.startsWith("delete")) {
            return Constant.DELETE;
        }

        return Constant.ERROR;
    }

    /**
     * Get a single query from input
     *
     * @param startPos
     * @return the ending position of this single query
     */
    public void getASingleQuery() {
        this.setSingleQuery(this.query);
        int len = query.length();
        for (int i = 1; i < len - 5; i++) {
            String q = query.substring(i, i + 6);
            if (q.equals("select")) {
                if (detectSelectLevel2(i)) {
                    setSingleQuery(getSelectQueryMulti(i + 6));
                    //sub it
                    this.query = this.query.substring(this.singleQuery.length(), len).trim();
                    return;
                }
            } else if (q.equals("select")
                    || q.equals("insert")
                    || q.equals("update")
                    || q.equals("delete")) {
                setSingleQuery(query.substring(0, i - 1));
                //sub it
                this.query = this.query.substring(this.singleQuery.length(), len).trim();
                return;
            }
        }
        //sub it
        this.query = this.query.substring(this.singleQuery.length(), len).trim();
    }

    private boolean detectSelectLevel2(int pos) {
        String s = this.query.substring(0, pos);
        if (s.contains(" in ") || s.contains(" existe ")) {
            return true;
        }

        return false;
    }

    private String getSelectQueryMulti(int pos) {
        for (int i = pos; i < this.query.length() - 5; i++) {
            String q = query.substring(i, i + 6);
            if (q.equals("select")
                    || q.equals("insert")
                    || q.equals("update")
                    || q.equals("delete")) {
                return this.query.substring(0, i);
            }
        }
        return this.query;
    }

    /**
     * @return the query
     */
    public String getQuery() {
        return query;
    }

    /**
     * @param query the query to set
     */
    public void setQuery(String query) {
        KeyWord kw = new KeyWord(query);
        this.query = kw.changKeyWordToLowerCase();
    }

    /**
     * @return the singleQuery
     */
    public String getSingleQuery() {
        return singleQuery;
    }

    /**
     * @param singleQuery the singleQuery to set
     */
    public void setSingleQuery(String singleQuery) {
        this.singleQuery = singleQuery;
    }
}
